package com.task.www.service;

import com.task.www.constant.ShowRegister;
import com.task.www.constant.ShowUser;
import com.task.www.dao.MechantData;
import com.task.www.dao.UserData;
import com.task.www.po.Food;
import com.task.www.po.Merchant;
import com.task.www.servlet.choose;
import com.task.www.servlet.RegisterNext;
import com.task.www.util.Clear;

import java.util.ArrayList;
import java.util.Scanner;

public class MainUser {
    public static void main(String[] args) {
        ShowRegister.showInterface(0);
        int number = choose.getnumber(1,2);
        new Clear();
        UserData data = new UserData();
        MechantData datamech = new MechantData();
        ShowUser show = new ShowUser();
        Scanner sn = new Scanner(System.in);
        String accout = RegisterNext.getaccout();
        String password = RegisterNext.getpassword();

        //注册账号
        if(number==1){
            System.out.println("\n");
            data.insertregister(accout,password);
        }

        //登录账号
        if(number==2){
            while(!data.selectaccout(accout,password)){
                System.out.println("\n账号密码错误，请重新输入");
                accout = RegisterNext.getaccout();
                password = RegisterNext.getpassword();
            }
        }

        int ids = data.selectids(accout);
        while(true){
            show.user(ids);
            int number1 = choose.getnumber(1,4);
            Clear.clear();
            if(number1==1) {
                while (true) {
                    ArrayList<String> merchants = data.selectmerchant();
                    show.merchants(merchants);
                   int number2 = choose.getnumber(1,merchants.size()/2+1);
                   if(number2==merchants.size()/2+1){
                       break;
                   }
                   else {
                       while (true) {
                           int idsmenchant = data.selectidsmer(merchants.get((number2 - 1) * 2));
                           ArrayList<String> merchant = data.selectmerchant(idsmenchant);
                           ArrayList<Integer> coupon = data.selectcoupon(idsmenchant);
                           ArrayList<Food> foods = data.selectfood(idsmenchant);
                           show.merchantinterface(merchant, coupon, foods);
                           int number3 = choose.getnumber(1, 3);
                           if (number3 == 1) {
                               //领取优惠券
                               Clear.clear();
                               if (data.havecoupon(ids, idsmenchant)) {
                                   System.out.println("你已经领取过了，不可重复领取了哦");
                               } else {
                                   data.getcoupon(ids,idsmenchant,coupon);
                                   System.out.println("领取成功");
                               }
                               System.out.println("三秒后返回上一页面");
                               try {
                                   Thread.sleep(3000);
                               } catch (InterruptedException e) {
                                   e.printStackTrace();
                               }
                               continue;
                           }
                           if (number3 == 2) {
                               //菜单页面
                               while(true){
                                   show.foods(foods);
                                   int number4 = choose.getnumber(1,foods.size()+1);
                                       if(number4==foods.size()+1){
                                           break;
                                       }else{
                                           while(true) {
                                               ArrayList<String> food = datamech.selectFood(idsmenchant, foods.get(number4 - 1).getName());
                                               show.foodbuy(food);
                                               int number5 = choose.getnumber(1, 3);
                                               if(number5==1){
                                                   //下单
                                                   int buyup = foods.get(number4-1).getPrice();
                                                   int[] updown = data.getdiscount(buyup,ids,idsmenchant);
                                                   show.buy(buyup,buyup-updown[1]);
                                                   int number6 = choose.getnumber(1,3);
                                                   if(number6==1||number6==2) {
                                                       if (number6 == 1) {
                                                           if(!data.buy(buyup - updown[1], ids, idsmenchant)) {
                                                               System.out.println("余额不足");
                                                           }else{
                                                               data.addorder(ids, idsmenchant, foods.get(number4 - 1).getName());
                                                               System.out.println("你也已下单成功");
                                                           }
                                                       }
                                                       if (number6 == 2) {
                                                           if(!data.buy(buyup , ids, idsmenchant)) {
                                                               System.out.println("余额不足");
                                                           }else{
                                                               data.addorder(ids, idsmenchant, foods.get(number4 - 1).getName());
                                                               System.out.println("你也已下单成功");
                                                           }
                                                       }
                                                       try {
                                                           System.out.println("三秒后自动跳转");
                                                           Thread.sleep(3000);
                                                       } catch (InterruptedException e) {
                                                           e.printStackTrace();
                                                       }
                                                   }
                                               }
                                               if(number5==2){
                                                   Clear.clear();
                                                   System.out.println("请输入你的评论：");
                                                   String comment = sn.next();
                                                   int foodid = data.getfoodid(idsmenchant,foods.get(number4 - 1).getName());
                                                   data.insertcomment(foodid,ids,comment);
                                                   try {
                                                       Thread.sleep(3000);
                                                       System.out.println("评论成功，三秒后返回上一页面");
                                                   } catch (InterruptedException e) {
                                                       e.printStackTrace();
                                                   }
                                               }
                                               if(number5==3){
                                                    break;
                                               }
                                           }
                                       }
                               }
                           }
                           if (number3 == 3) {
                               break;
                           }
                       }
                   }
                }
                continue;
            }
            if(number1==2){
                System.out.println("请输入要充值的金额：");
                int monneyadd = sn.nextInt();
                data.updatemonney(ids,monneyadd);
                Clear.clear();
                System.out.println("充值成功，三秒后返回上一页面");
                try {
                    Thread.sleep(3000);
                    continue;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            if(number1==3){
                show.order(data.checkorder(ids));
                sn.next();
            }
            if(number1==4){
                 break;
            }

        }
    }
}
